import type { Config } from '@alova/wormhole'

// For more config detailed visit:
// https://alova.js.org/tutorial/getting-started/extension-integration

export default <Config>{
  generator: [
    {
      /**
       * file input. support:
       * 1. openapi json file url
       * 2. local file
       */
      input: 'http://127.0.0.1:4523/export/openapi/11?version=3.0',

      /**
       * input file platform. Currently only swagger is supported.
       * When this parameter is specified, the input field only needs to specify the document address without specifying the openapi file
       */
      platform: 'swagger',

      /**
       * output path of interface file and type file.
       * Multiple generators cannot have the same address, otherwise the generated code will overwrite each other.
       */
      output: 'src/api',

      /**
       * the mediaType of the generated response data. default is `application/json`
       */
      // responseMediaType: 'application/json',

      /**
       * the bodyMediaType of the generated request body data. default is `application/json`
       */
      // bodyMediaType: 'application/json',

      /**
       * the generated api version. options are `2` or `3`, default is `auto`.
       */
      // version: 'auto',

      /**
       * type of generated code. The options are `auto/ts/typescript/module/commonjs`.
       */
      // type: 'auto',

      /**
       * exported global api name, you can access the generated api globally through this name, default is `Apis`.
       * it is required when multiple generators are configured, and it cannot be repeated
       */
      // global: 'Apis',

      /**
       * filter or convert the generated api information, return an apiDescriptor, if this function is not specified, the apiDescripor object is not converted
       */
      handleApi: (apiDescriptor) => {
        console.log('apiDescriptor', apiDescriptor)
        let modifiedUrl = apiDescriptor.url
        const paths = modifiedUrl.split('/').filter((path) => {
          return !(apiDescriptor.tags && apiDescriptor.tags.includes(path))
        })
        const operationId = paths
          .map((path, index) => {
            if (index === 0) {
              return path.charAt(0).toLowerCase() + path.slice(1)
            } else if (path.includes('{')) {
              // 直接舍弃这一组
              return ''
            } else {
              return path.charAt(0).toUpperCase() + path.slice(1)
            }
          })
          .join('')

        // if (apiDescriptor.method === 'get') {
        //   operationId =
        //     'fetch' + operationId.charAt(0).toUpperCase() + operationId.slice(1)
        // }

        // TODO: 这里需要根据项目实际地址修改为对应的地址，operate只是暂时使用
        modifiedUrl = '/spo' + modifiedUrl

        const modifiedApiDescriptor = {
          ...apiDescriptor,
          url: modifiedUrl,
          operationId,
        }

        const newResponse = modifiedApiDescriptor.responses?.properties?.data
        modifiedApiDescriptor.responses = newResponse

        return modifiedApiDescriptor
      },
    },
  ],

  /**
   * extension only
   * whether to automatically update the interface, enabled by default, check every 5 minutes, closed when set to `false`
   */
  // autoUpdate: true
}
